<template>
    <div>
        <el-form ref="form" :model="form" label-width="100px" :rules="rules">
            <el-form-item label="封面：">
                <el-upload :disabled="form.coverPath !== ''&&form.coverPath !== null" :file-list="fileList" :multiple="false" drag ref="uploadCover" action="#" 
                list-type="picture" :auto-upload="false" class="upload" accept="image/*" :on-change="handleChange" :on-remove="handleRemove">
                    <div v-if="form.coverPath === ''||form.coverPath === null" style="display: flex;flex-direction: column;align-items: center;">
                        <img :src="require('@/assets/article/uploadimage.png')" alt="Upload Image" class="upload_image" />
                        <div class="el-upload__text upload_tip">
                            拖拽图片到此区域也可上传<span><em>上传</em>一张不超过 2MB 的image/*文件</span>
                        </div>
                    </div>
                    <div v-else class="url_image">
                        <el-image :preview-src-list="[form.coverPath.url||form.coverPath]" :src="form.coverPath.url||form.coverPath" class="url_img" ref="urlImg"></el-image>
                        <div class="url_drow">
                            <i class="el-icon-delete" @click.stop="handleRemove"></i>
                            <i class="el-icon-zoom-in" @click="handlePreview"></i>
                        </div>
                    </div>
                </el-upload>
            </el-form-item>
            <el-form-item prop="src" label="来源：">
                <el-input style="width: 80%;" v-model.trim="form.src"></el-input>
            </el-form-item>
            <el-form-item prop="author" label="作者：">
                <el-input style="width: 80%;" v-model.trim="form.author"></el-input>
            </el-form-item>
            <el-form-item prop="articleAbstract" label="文章摘要：">
                <el-input type="textarea" style="width: 80%;" v-model.trim="form.articleAbstract" maxlength="120" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="浏览权限：">
                <el-radio-group v-model="form.permType">
                    <el-radio :label="0">默认</el-radio>
                    <el-radio :label="1">自定义</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="文章分类：" prop="categoryId">
                <!-- <el-select multiple style="width:50%;" v-model="setting.type" placeholder="请选择文章分类">
                    <el-option v-for="item in typeData" :key="item.id" :label="item.catName" :value="item.id"></el-option>
                </el-select> -->
                <el-cascader @change="handleChangeType" v-model="form.categoryId" style="width:50%;" 
                :options="typeData" change-on-select clearable
                :props="selectDataProps"></el-cascader>
                <el-button @click="activeInto" type="primary" size="mini" style="line-height:25px;margin-left: 10px;">
                    管理分类
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import { getArticleCategoryList } from '@/api/api.js'
export default {
    props:{
        form:{
            type:Object,
            default:{}
        }
    },
    data() {
        return {
            // setting: {
            //     src: '',
            //     author: '',
            //     category: '',
            //     role:'1',
            //     type:[],
            //     cover: [],
            // },
            fileList:[],
            selectDataProps:{ 
                children: 'children', 
                value: 'id', 
                label: 'catName' 
            },
            searchList:{
                catName:'',
                parentId:''
            },
            url:'',
            rules: {
                src:[
                    { required: true, message: "来源不能为空", trigger: "blur"}
                ],
                author: [
                    { required: true, message: "作者不能为空", trigger: "blur" },
                ],
                articleAbstract: [
                    { required: true, message: "文章摘要不能为空", trigger: "blur" },
                ],
                categoryId: [
                    { required: true, message: "文章分类不能为空", trigger: "blur" },
                ],
            },
            typeData:[]
        }
    },
    methods: {
        // fieldBeforeUpload(file) {
        //     let isRightSize = file.size / 1024 / 1024 < 2
        //     if (!isRightSize) {
        //         this.$message.error('文件大小超过 2MB')
        //     }
        //     let isAccept = new RegExp('image/*').test(file.type)
        //     if (!isAccept) {
        //         this.$message.error('应该选择image/*类型的文件')
        //     }
        //     return isRightSize && isAccept
        // },

        // 选择文件时，往fileList里添加
        handleChange(file, fileList) {
            this.$refs.uploadCover.clearFiles();
            let isRightSize = file.size / 1024 / 1024 < 2;
            if (!isRightSize) {
                this.$message.error('文件大小超过 2MB');
                return;
            }
            let isAccept = new RegExp('image/*').test(file.raw.type)
            if (!isAccept) {
                this.$message.error('应该选择image/*类型的文件');
                return;
            }
            this.form.coverPath = file;
            this.fileList[0] = file;
        },

        // 删除文件
        handleRemove(file, fileList) {
            const el = this.$refs.urlImg.$el;
            el.classList.add('flipOutX');
            setTimeout(() => {  
                // 移除效果
                el.classList.remove('flipOutX');  
                this.fileList = [];
                this.$refs.uploadCover.clearFiles();
                this.form.coverPath = ''; 
            }, 500);
        },

        // 预览
        handlePreview(){
            this.$refs.urlImg.showViewer = true;
        },

        // 管理分类
        activeInto(){
            this.$emit('activeInto', false);
        },

        // 获取文章类型列表
        async getArticleCategoryList(){
            try{
                const list = await getArticleCategoryList(this.searchList);
                if(list.data.code == 200){
                    this.typeData = list.data.result;
                }else{
                    this.$message({message: '文章分类列表获取失败！',type: 'error'});
                }
            }catch(e){
                this.$message({message: '处理失败！',type: 'error'});
                console.log(e)  
            }
        },

        // 分类选择处理
        handleChangeType(){
            let categoryId = this.form.categoryId;
            if(categoryId){
                this.form.categoryId = categoryId[categoryId.length - 1];
            };
        }
    },
    async mounted(){
        await this.getArticleCategoryList();
        // this.setting.type.push(this.typeData[0].id);
    },
    async activated(){await this.getArticleCategoryList();},
    computed:{
    },
}
</script>
<style scoped>
.upload >>>.el-upload-dragger{
  opacity: 1;
  border-radius: 10px;
  background: rgba(253, 252, 255, 1);
  border: 1px dashed rgba(126, 114, 242, 1);
  /* z-index: 1; */
}
.upload >>> .el-upload--picture-card{
    width: auto;
    border-radius: 10px;
    height: auto;
}
.upload {
  position: relative; 
  height: 170px;
  /* z-index: 1; */
}
.upload_image{
    width: 50px;
    height: 50px;
    margin-top: 30px;
}
.upload_content{
    display: flex;
    flex-direction: column;

}
.upload_tip{
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    line-height: 20px;
    justify-content: center;
}
.url_drow{
    display: none;
    color: white;
    top: 0;
    width: 100%;
    height: 100%;
    cursor:default;
    position: absolute;
    background-color: rgba(118, 116, 116, 0.589);
    justify-content: center;
    align-items: center;
    font-size: 25px;
}
.url_drow i{
    cursor: pointer;
    margin-left: 5px;
    margin-right: 5px;
}
.url_drow i:hover{
    color:rgb(126, 114, 242);
}
.url_image:hover .url_drow{
    display: flex;
}
.url_img{
    width: 100%;
    height: 180px;
    animation-duration: 0.5s;
    animation-fill-mode: both;
    animation-name: fadeInDown;
    backface-visibility: visible !important;
}
.el-button--primary{
    background-color: rgb(126, 114, 242);
    border-color: #7E72F2;
}
.el-button--primary:hover{
    background-color: rgba(126, 114, 242, 0.5);
}
@keyframes fadeInDown {
    from {
        transform: perspective(400px) rotate3d(1, 0, 0, 90deg);
        animation-timing-function: ease-in;
        opacity: 0;
    }

    40% {
        transform: perspective(400px) rotate3d(1, 0, 0, -20deg);
        animation-timing-function: ease-in;
    }

    60% {
        transform: perspective(400px) rotate3d(1, 0, 0, 10deg);
        opacity: 1;
    }

    80% {
        transform: perspective(400px) rotate3d(1, 0, 0, -5deg);
    }

    to {
        transform: perspective(400px);
    }
}
@keyframes flipOutX {
    from {
        transform: perspective(400px);
    }

    30% {
        transform: perspective(400px) rotate3d(1, 0, 0, -20deg);
        opacity: 1;
    }

    to {
        transform: perspective(400px) rotate3d(1, 0, 0, 90deg);
        opacity: 0;
    }
}

.flipOutX {
    animation-duration: calc(0.8s * 0.75);
    animation-name: flipOutX;
    backface-visibility: visible !important;
}
/* /deep/ .el-cascader-panel .el-scrollbar:first-child .el-checkbox,.el-scrollbar:nth-child(2) .el-checkbox{
      display: none;
} */
</style>